/**
 * Copyright 2013 Matt Holland <code.monkey.ota at gmail.com>
 *
 * This file is part of BotTender.
 *
 * BotTender is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BotTender is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BotTender. If not, see <http://www.gnu.org/licenses/>.
 */

package ota.ircbot.bottender.admin;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.PrivateMessageEvent;

import ota.ircbot.config.BotConfig;

public class ChannelComms extends ListenerAdapter {
	
	private PircBotX bot;
	private BotConfig cfg;
	
	public ChannelComms(PircBotX botInst, BotConfig conf) {
		bot = botInst;
		cfg = conf;
	}
	
	public void onPrivateMessage(PrivateMessageEvent event) {
		if(Authorized.isOwner(event.getUser())){
			if(event.getMessage().matches("^(?i:\\.join) .*")) {
				joinChans(multiChanSplit(event.getMessage()));
			}
			else if(event.getMessage().matches("^(?i:\\.leave) .*")) {
				leaveChans(multiChanSplit(event.getMessage()));
			}
			else if(event.getMessage().matches("^(?i:\\.add chan(nels)*) .*")) {
				String chans = addChans(multiChanSplit(event.getMessage()));
				event.getUser().sendMessage(chans+ "added to the autoJoin list.");
			}
		}
		else {
			event.respond("Owner required for this command.");
		}
	}
	private void joinChans(List<String> channels) {
		Iterator<String> iter = channels.iterator();
		while(iter.hasNext()) bot.joinChannel(iter.next());
	}
	
	//Identical function, but for leaving channels
	private void leaveChans(List<String> channels) {
		Iterator<String> iter = channels.iterator();
		while(iter.hasNext()) bot.partChannel(bot.getChannel(iter.next())); // PircBotX.partChannel() needs a Channel object
		// in order to function, so we grab one using the channel name provided.
	}
	
	private List<String> multiChanSplit(String line) {
		line = line.replaceAll("^(?i:(\\.join|\\.leave|\\.add chan(nels)*)) " , ""); // Remove either .join or .leave from the message
		List<String> res = Arrays.asList(line.split(",\\S*")); // Split the string up based on commas and possible space.
		Iterator<String> it = res.iterator();
		while(it.hasNext()) { // While there is another item in the list...
			if(!it.next().startsWith("#")) it.remove(); // If it doesn't start with a '#' remove it.
		}
		return res; // Return whatever is left.
	}
	
	private String addChans(List<String> channels) {
		Iterator<String> it = channels.iterator();
		String chans = ""; // This is the channels with a comma delimiter.
		while(it.hasNext()) {
			chans+= it.next() +", ";
		}
		cfg.setOption("Channels", chans);
		return chans;
	}

}
